//
//  WKCustomTableViewCell.m
//  WKDemo
//
//  Created by apple on 14-8-3.
//  Copyright (c) 2014年 com.app. All rights reserved.
//

#import "WKCustomTableViewCell.h"
#import "WKVidelModel.h"
#import "UIImageView+WebCache.h"
#import "WKVideoModelFrame.h"
#import "WKStatusToolbar.h"
#import "MDRadialProgressView.h"
#import "MDRadialProgressTheme.h"
#import "TaichiView.h"
#import "THProgressView.h"

@interface WKCustomTableViewCell ()
@property (nonatomic,weak)UIButton *playButton;
@property (nonatomic,weak)UIImageView *showView;
@property (nonatomic,strong)MPMoviePlayerController *mpc;
@property (nonatomic,weak)UIProgressView *playProgressSlider;
@property (nonatomic,weak)THProgressView *thprogress;
@property (nonatomic,copy)NSString *totaltime;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UILabel *textsLabel;
@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, weak) UIImageView *iconView;
@property (nonatomic,strong)WKStatusToolbar *toolBar;
@property (nonatomic, weak) UILabel *playCountLabel;
@property (nonatomic, weak) UILabel *playTimeLabel;
@property (nonatomic,strong)NSTimer *timer;
@property (nonatomic,strong)TaichiView *taiji;
@property (nonatomic,assign)double current;
@end
@implementation WKCustomTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //取消显示选中时的阴影
//        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.font = [UIFont systemFontOfSize:11.0];
        [self addSubview:nameLabel];
        self.nameLabel = nameLabel;

        // 2.正文（内容）
        UILabel *textLabel = [[UILabel alloc] init];
        textLabel.font = [UIFont systemFontOfSize:14.0];
        textLabel.numberOfLines = 0;
        [self addSubview:textLabel];
        self.textsLabel = textLabel;
        
        // 3.时间
        UILabel *timeLabel = [[UILabel alloc] init];
        timeLabel.textColor = [UIColor orangeColor];
        timeLabel.font = [UIFont systemFontOfSize:11.0];
        [self addSubview:timeLabel];
        self.timeLabel = timeLabel;
        
        // 5.头像
        UIImageView *iconView = [[UIImageView alloc] init];
      iconView.layer.cornerRadius = 8;
        iconView.clipsToBounds = YES;
        [self addSubview:iconView];
        self.iconView = iconView;
        

        UIImageView *showView = [[UIImageView alloc] init];
        showView.userInteractionEnabled = YES;
        [self addSubview:showView];
        self.showView = showView;
        
        
        UIButton *btn = [[UIButton alloc] init];
       // btn.backgroundColor = [UIColor redColor];
        [btn setImage:[UIImage imageNamed:@"repeatVideoStarticon-1"] forState:UIControlStateNormal];
        //[btn setTitle:@"播放" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(clickPlayButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.showView addSubview:btn];
        self.playButton = btn;
        
        UIProgressView *slider = [[UIProgressView alloc] init];
        slider.tintColor = [UIColor redColor];
        [self addSubview:slider];
        self.playProgressSlider = slider;
        
        MPMoviePlayerController *mpc = [[MPMoviePlayerController alloc] init];
        mpc.controlStyle = MPMovieControlStyleNone;
        mpc.repeatMode = MPMovieRepeatModeOne;
        [self addSubview:mpc.view];
        self.mpc = mpc;
        
        self.mpc.view.hidden = YES;
       
        WKStatusToolbar *bar = [[WKStatusToolbar alloc] init];
        
        //bar.delegate = self;
        [self addSubview:bar];
        self.toolBar = bar;

        UILabel *playCountLabel = [[UILabel alloc] init];
        playCountLabel.backgroundColor = WKColorAlpha(0, 0, 0, 0.3);
        playCountLabel.textColor = [UIColor whiteColor];
        playCountLabel.font = [UIFont systemFontOfSize:11.0];
        [self addSubview:playCountLabel];
        self.playCountLabel = playCountLabel;
        
        UILabel *playTimeLabel = [[UILabel alloc] init];
        playTimeLabel.backgroundColor = WKColorAlpha(0, 0, 0, 0.3);
        playTimeLabel.textColor = [UIColor whiteColor];
        playTimeLabel.textAlignment = NSTextAlignmentCenter;
        playTimeLabel.font = [UIFont systemFontOfSize:11.0];
        [self addSubview:playTimeLabel];
        self.playTimeLabel = playTimeLabel;
        
        TaichiView *taiji = [[TaichiView alloc ] init];
        taiji.hidden = YES;
        [self addSubview:taiji];
        self.taiji = taiji;
        
        THProgressView *progress = [[THProgressView alloc] init];
        //progress.frame = CGRectMake(50, 50, 200, 20);
        progress.progressTintColor = [UIColor darkGrayColor];
        progress.borderTintColor = [UIColor lightGrayColor];
       // progress.progress = ((CGFloat)receivedSize)/expectedSize;
        [self.showView addSubview:progress];
        self.thprogress = progress;
        self.thprogress.hidden= YES;
    }
    return self;
}

- (void)setVideoFrame:(WKVideoModelFrame *)videoFrame
{
   
    _videoFrame = videoFrame;
   
//    [self.iconView setImageWithURL:[NSURL URLWithString:videoFrame.videoModel.profile_image] placeholderImage:[UIImage imageNamed:@"nav-order-nearby-click"]];
    //设置头像
    UIImageView *imageV = [[UIImageView alloc]init];
    [imageV setImageWithURL:[NSURL URLWithString:videoFrame.videoModel.profile_image] placeholderImage:[UIImage imageNamed:@"placeholderImage1"]];
    UIImage *oldImage = imageV.image;
    
    self.iconView.image = [UIImage circleImageWithName:oldImage borderWidth:1 borderColor:[UIColor black75PercentColor]];
    self.iconView.frame = self.videoFrame.iconFrame;
  
    //昵称
    self.nameLabel.text = videoFrame.videoModel.name;
    self.nameLabel.frame = self.videoFrame.nameFrame;
    //时间
    self.timeLabel.text = videoFrame.videoModel.passtime;
    self.timeLabel.frame = self.videoFrame.timeFrame;
   //正文
    self.textsLabel.text = videoFrame.videoModel.text;
    self.textsLabel.frame = self.videoFrame.textFrame;
    
    //[self.showView setImageWithURL:[NSURL URLWithString:self.videoFrame.videoModel.cdn_img]];
    //视频
    [self.showView setImageWithURL:[NSURL URLWithString:self.videoFrame.videoModel.cdn_img] placeholderImage:nil options:nil progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        self.thprogress.hidden = NO;
        self.thprogress.frame = CGRectMake(50, 50, 200, 20);
        self.thprogress.progress = ((CGFloat)receivedSize)/expectedSize;
            } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
              
        self.thprogress.hidden = YES;
        [self.thprogress removeFromSuperview];
    }];
    self.showView.frame = self.videoFrame.showViewFrame;
    
    self.mpc.contentURL = [NSURL URLWithString:self.videoFrame.videoModel.videouri];
    self.mpc.view.frame = self.videoFrame.playViewFrame;
    
    self.playButton.frame = self.videoFrame.buttonFrame;
    
    self.playProgressSlider.frame = self.videoFrame.sliderFrame;
    
    self.totaltime = self.videoFrame.videoModel.videotime;
    
    self.toolBar.frame = self.videoFrame.toolbarFrame;
    self.toolBar.videos = videoFrame.videoModel;
    
    self.playCountLabel.text = [NSString stringWithFormat:@"%@次播放",videoFrame.videoModel.playcount];
    self.playCountLabel.frame =self.videoFrame.playCountLabelFrame;
    
    self.playTimeLabel.frame = self.videoFrame.playTimeLabelFrame;
    int videotime = self.videoFrame.videoModel.videotime.intValue;
    NSString *time = nil;
    if (videotime>60) {
       time = [NSString stringWithFormat:@"%0d:%02d",videotime/60,videotime%60];
    }else
    {
       time = [NSString stringWithFormat:@"00:%02d",videotime];
    }
    self.playTimeLabel.text = time;
    
    self.taiji.frame = [self convertRect:self.playButton.frame fromView:self.showView];
}

- (void)clickPlayButton:(UIButton *)btn
{
    self.showView.hidden = YES;
    self.mpc.view.hidden = NO;
    [self.mpc play];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playDidFinish) name:MPMoviePlayerPlaybackDidFinishNotification object:self.mpc];
   self.timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(a) userInfo:nil repeats:YES];

}
- (void)a
{
  
 
    if (self.mpc.currentPlaybackTime == self.current) {
        
        self.taiji.hidden = NO;
        
    }else
    {
        self.taiji.hidden =YES;
        
     self.current = self.mpc.currentPlaybackTime;
    }
    
    
    self.playProgressSlider.progress = self.mpc.currentPlaybackTime/self.totaltime.intValue;
    if (self.mpc.currentPlaybackTime>60) {
         self.playTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d",(int)self.mpc.currentPlaybackTime/60,(int)self.mpc.currentPlaybackTime%60];
    }else
    {
    self.playTimeLabel.text = [NSString stringWithFormat:@"00:%02d",(int)self.mpc.currentPlaybackTime];
    }
}

- (void)playDidFinish
{
    self.showView.hidden= NO;
    
    [self.mpc.view removeFromSuperview];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidEnterFullscreenNotification object:self.mpc];
}
- (void)removePlay
{
    [self.toolBar.subviews makeObjectsPerformSelector:@selector(setEnabled:) withObject:self];
    for (UIButton *btn in self.toolBar.subviews) {
        btn.enabled = YES;
        
    }
    [self.timer invalidate];
    self.timer = nil;
    self.mpc.view.hidden = YES;
    self.showView.hidden = NO;
    self.taiji.hidden = YES;
    [self.mpc pause];
  
}

+ (instancetype)cellWithtableView:(UITableView *)tableView
{
static NSString *ID = @"customCell";
    WKCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
  
    return cell;
}
@end
